package com.fyl.controller;


import com.fyl.entity.Orders;
import com.fyl.entity.User;
import com.fyl.service.CartService;
import com.fyl.service.OrdersService;
import com.fyl.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 馮永龍
 * @since 2020-11-27
 */
@Controller
@RequestMapping("//orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private CartService cartService;

    @GetMapping("/list")
    public ModelAndView getAllByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orderList");
        modelAndView.addObject("orderList", ordersService.getAllByUserId(user.getId()));
        modelAndView.addObject("carts", this.cartService.findAllVO(user.getId()));
        return modelAndView;
    }

    @PostMapping("/create")
    public ModelAndView create(String selectAddress, Float cost, String address, String remark, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Orders orders = this.ordersService.creat(selectAddress, cost, user, address, remark);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement3");
        modelAndView.addObject(orders);
        modelAndView.addObject("carts", this.cartService.findAllVO(user.getId()));
        return modelAndView;
    }

}

